﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DataAccess.Administration.Catalog;
using System.Configuration;
using SIPO.Web.Support;
using SIPO.Library.DomainObject.Administration.UserManagement;

namespace SIPO.Web.Forms.Administration.Catalog
{
   public partial class recSubSectorform : BasePage
   {
        private int id = -1;
        
        protected override void OnLoad(EventArgs e)
        {
            recSectorRepo recSectorDal = new recSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
            recBankAccountRepo recBankAccountDal = new recBankAccountRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

            List<recSector> sectors = recSectorDal.GetList();
            ddlSector.Items.Clear();
            sectors.ForEach(delegate(recSector sector)
            {
                ddlSector.Items.Add(new ListItem(sector.SectorCode + " - " + sector.SectorName, sector.SectorID.ToString()));
            });

            ddlSector.Items.Insert(0, new ListItem("-- pilih sektor --", string.Empty));


            List<recBankAccount> banks = recBankAccountDal.GetList();
            ddlBank.Items.Clear();
            banks.ForEach(delegate(recBankAccount bank)
            {
                ddlBank.Items.Add(new ListItem(bank.BankCode + " - " + bank.BankName, bank.BankID.ToString()));
            });

            ddlBank.Items.Insert(0, new ListItem("-- pilih bank --", string.Empty));


            if (Request.Params["id"] != null)
            {
                id = Convert.ToInt32(Request.Params["id"]);
            }

            if (!Page.IsPostBack)
            {
                if (id > 0) // Edit an existing item
                {
                    recSubSectorRepo recSubSectorDal = new recSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    recSubSector recSubSector = recSubSectorDal.GetById(id);
                    if (recSubSector != null)
                    {
                        hfID.Value = id.ToString();
                        txtCode.Text = recSubSector.SubSectorCode;
                        txtName.Text = recSubSector.SubSectorName;
                        ddlSector.Items.FindByValue(recSubSector.SectorID.ToString()).Selected = true;
                        ddlBank.Items.FindByValue(recSubSector.BankID.ToString()).Selected = true;

                        ddlSector.Enabled = false;
                        txtCode.ReadOnly = true;
                    }
                }
                GetPrivilege();
            }
        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').button('disable');", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }

   }
}
